using CommonDomain.Core;
using CommonDomain.Persistence;
using CommonDomain.Persistence.EventStore;
using EventStore;
using EventStore.Dispatcher;
using StructureMap.Configuration.DSL;

namespace Sponsorship.Infrastructure.BootStrapping.Registries
{
    /// <summary>
    /// Registers the event store (for storing instances of domain events published by the domain) with StructureMap.
    /// </summary>
    public class EventStoreRegistry : Registry
    {
        public EventStoreRegistry()
        {
            //Bus
            var bus = new InProcessBus();
            For<IBus>().Use(bus);

            var eventStore = GetInitializedEventStore(bus);
            var repository = new EventStoreRepository(eventStore, new AggregateFactory(), new ConflictDetector());

            For<IStoreEvents>().Use(eventStore);
            For<IRepository>().Use(repository);
        }

        private IStoreEvents GetInitializedEventStore(IDispatchCommits bus)
        {
            return Wireup.Init()
                .UsingRavenPersistence(BootStrapper.RavenDbConnectionStringName)
                .UsingSynchronousDispatchScheduler(bus)
                .Build();
        }
    }
}